export module LyTools {
    /**
    * 获取两点间的运动时间
    * @param pos1 起始点
    * @param pos2 终点
    * @param speed 速度
    * @returns 运动时间
    */
    export function getMoveTime(pos1: cc.Vec2, pos2: cc.Vec2, speed: number) {
        let roundLen = Math.sqrt(Math.pow(pos1.x - pos2.x, 2) + Math.pow(pos1.y - pos2.y, 2))
        return roundLen / speed
    }
    /**
     * 获得两个点路径上的随机一点
     * @param pos1 起始点
     * @param pos2 终点
     */
    export function getRandomPos(pos1: cc.Vec2, pos2: cc.Vec2) {
        let k = (pos2.y - pos1.y) / (pos2.x - pos1.x)
        let b = pos2.y - k * pos2.x
        let ranX = LyTools.randomNum(pos1.x, pos2.x)
        let ranY = k * ranX + b
        return cc.v2(ranX, ranY)
    }

    export function randomNum(minNum: number, maxNum: number) {
        switch (arguments.length) {
            case 1:
                return parseInt((Math.random() * minNum + 1).toString(), 10);
            case 2:
                return parseInt((Math.random() * (maxNum - minNum + 1) + minNum).toString(), 10);
            default:
                return 0;
        }
    }
    //转换为世界坐标系
    export function localConvertWorldPointAR(node) {
        if (node) {
            return node.convertToWorldSpaceAR(cc.v2(0, 0));
        }
        return null;
    }

    /**
     * 把一个世界坐标的点，转换到某个节点下的坐标
     * 原点在node中心
     * @param {*} node 
     * @param {*} worldPoint 
     */
    export function worldConvertLocalPointAR(node, worldPoint) {
        if (node) {
            return node.convertToNodeSpaceAR(worldPoint);
        }
        return null;
    }

    /**
   * 
   * @param name 消息命名
   * @param target 信息
   * @param targetNdoe 发送消息节点
   */
    export function dispatchEventServer(name: string, target: any, targetNdoe: cc.Node) {
        let eventItem = new cc.Event.EventCustom(`${name}`, true);
        eventItem.detail = target
        targetNdoe.dispatchEvent(eventItem);
    }

    // 补零
    export function prefixInteger(num, n) {
        if (String(num).length > n) {
            return num
        }
        return (Array(n).join('0') + num).slice(-n);
    }
    // 时间转字符
    export function secTostr(v) {
        let min = parseInt((v / 60).toString())
        let sec = v % 60
        let str = `${this.prefixInteger(min, 2)}:${this.prefixInteger(sec, 2)}`
        return str
    }


    /**
    * @param {*} spine
    * @param {*} animName 动画名称
    * @param {*} loop 是否循环
    * @param {*} callback 放完动画的回调
    */
    export function playSpine(spineSke, animName: string, loop: boolean, callback?: Function) {
        let spine = spineSke
        let track = spine.setAnimation(0, animName, loop);
        if (track) {
            // 注册动画的结束回调
            spine.setCompleteListener((trackEntry, loopCount) => {
                let name = trackEntry.animation ? trackEntry.animation.name : '';
                if (name === animName && callback) {
                    this.canClick = true
                    callback(); // 动画结束后执行自己的逻辑
                }
            });
        }
    }

    export function hideNode(item: cc.Node, time?: number) {
        cc.tween(item)
            .to(time ? time : 0.5, { opacity: 0 })
            .call(() => {
                item.active = false
            })
            .start()
    }

    export function showItem(item: cc.Node) {
        item.active = true
        item.opacity = 255
    }

    export function gradientItem(item: cc.Node, time?: number) {
        item.active = true
        cc.tween(item)
            .to(time ? time : 1, { opacity: 255 })
            .start()
    }

    export function spineMix(spine: sp.Skeleton, grp: any) {
        for (let i = 0; i < grp.length; i++) {
            spine.setMix(grp[i].name1, grp[i].name2, grp[i].mixtime)
        }
    }
    /**
     * 生成相应颜色的纹理
     * @param color cc.v3(0,0,0)
     * @returns 纹理
     */
    export function createTexture(color: cc.Color) {
        let texture = new cc.Texture2D;
        texture.initWithData(new Uint8Array([color.r, color.g, color.b, color.a]), cc.Texture2D.PixelFormat.RGB888, 1, 1);
        return texture
    }
    /**
     * 生成单色的spriteFrame
     * @param color 颜色
     * @param rect 图片坐标以及长宽（x,y,weight,height）
     * @returns 
     */
    export function createSpriteFrame(color: cc.Color, rect: cc.Vec4) {
        let texture = this.createTexture(color)
        let spriteFrame = new cc.SpriteFrame;
        spriteFrame.setTexture(texture);
        spriteFrame.setRect(cc.rect(rect.x, rect.y, rect.z, rect.w));
        return spriteFrame
    }
    /**
     * 生成一个纯色节点
     * @param name 节点名称
     * @param color 颜色
     * @param size 大小
     * @returns 节点
     */
    export function createPureColorNode(name: string, color: cc.Color, size: cc.Vec2) {
        let item = new cc.Node(`${name}`)
        item.addComponent(cc.Sprite).spriteFrame = this.createSpriteFrame(color, new cc.Vec4(0, 0, size.x, size.y))
        return item
    }



}